package com.origin.onlinesys.web.controllers.modules.account;

import com.origin.onlinesys.common.controllers.BaseController;
import com.origin.onlinesys.domain.modules.interviewsys.Account;
import com.origin.onlinesys.service.modules.user.impl.AccountServiceImpl;
import com.origin.onlinesys.web.utils.WebUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by origin on 15-3-27.
 */
@Controller
@RequestMapping(value = "/management/userHandle")
public class UserHandle extends BaseController{
    @Autowired
    AccountServiceImpl accountService;

    @Autowired
    DefaultPasswordService passwordService;

    @RequestMapping(value = "/getUserName")
    public @ResponseBody
    Map getUserName(){
        Map map = new HashMap();
        map.put("name",WebUtils.getAccount().getRealname());
        return map;
    }

    @RequestMapping(value = "/loginOut")
    public @ResponseBody
    String loginOut(){
//        .logout();
//        SecurityUtils.getSubject().
        SecurityUtils.getSubject().logout();
//        SecurityUtils.getSecurityManager().logout(SecurityUtils.getSubject());
        return "true";
    }

    @RequestMapping(value = "/getUserInfo")
    public @ResponseBody
    Account getUserInfo(){
        return WebUtils.getAccount();
    }

    @RequestMapping(value = "/updatePwd")
    public ModelAndView updatePwd(){
        return new ModelAndView("pages/system/updatePwd","userName",
                WebUtils.getAccount().getRealname());
    }

    @RequestMapping(value = "/remind")
    public String remind(){
        return "remind";
    }

    @RequestMapping(value = "/updatePwdData")
    public @ResponseBody
    String updatePwd(String pwd,String newPwd){
        String encryptPwd = passwordService.encryptPassword(pwd);
        String encryptNewPwd = passwordService.encryptPassword(newPwd);
        return accountService.updatePwd(WebUtils.getAccount().getUsername(),
                encryptPwd,encryptNewPwd);
    }
}
